package aad.storyboard.view;

import jxl.format.Orientation;
import utils.ViewUtils;
import aad.storyboard.R;
import aad.storyboard.controller.MenuTouchListener;
import aad.storyboard.controller.MenuViewProvider;
import aad.storyboard.model.Story;
import aad.storyboard.model.menu.Menu;
import aad.storyboard.model.menu.MenuItem;
import aad.storyboard.model.menu.MenuSettings;
import aad.storyboard.model.menu.PlainCentralizedMenuBuilder;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MenuViewCreator{
	
	private static MenuViewCreator instance = new MenuViewCreator();
	private MenuTouchListener listener;
	private Activity cont;
	
	
	private MenuViewCreator(){
		
	}

	public static MenuViewCreator getInstance(Activity c){
		MenuViewProvider provider = MenuViewProvider.getInstance();
		instance.cont = c;
		instance.listener = MenuTouchListener.getInstance(c, provider);
		return instance;
	}
	
	public View createMenuView(Menu menu){

	    RelativeLayout menuLayout = new RelativeLayout(cont);
		menuLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
		//menuLayout.removeAllViews();
		menuLayout.setBackgroundColor(menu.getColor());
		menuLayout.setBackgroundResource(menu.getBackground());
		//menuLayout.setBackgroundDrawable(menu.getBackground());
		
		//first insert the title
		this.addTextView(menu, menuLayout, menu.getTitle(), false,true);
		
		
		//now add text if existing
		if(menu.getText() != null){
			this.addTextView(menu, menuLayout, menu.getText(), false,true);
		}
		
		//now add all the items
		MenuSettings settings = menu.getMenuSettings();
		LinearLayout ll = new LinearLayout(cont);
		ll.setOrientation(settings.getOrientation());
		ll.setGravity(settings.getGravity());
		for(MenuItem item : menu.getSelectionItems()){
			if(item.getType() == MenuItem.TEXTBUTTON)
				this.addTextView(menu, ll, item, true,false);
			else if(item.getType() == MenuItem.ADJUSTMENT)
				this.addAdjustmentView(item, menu, ll);
			else if(item.getType() == MenuItem.SWITCH)
				this.addSwitchView(item, menu, ll);
		}
		int width = LayoutParams.WRAP_CONTENT;
		int height = LayoutParams.WRAP_CONTENT;
		int mHeight = 0;
		int mWidth = 0;
		if(settings.getHeight() > 0)
			mHeight = height = ViewUtils.percentHeightToPixels(settings.getHeight());
		if(settings.getWidth() > 0)
			mWidth =width = ViewUtils.percentWidthToPixels(settings.getWidth());
		RelativeLayout.LayoutParams pr = new RelativeLayout.LayoutParams(width,height);
		//pr.gravity = menu.getMenuSettings().getGravity();

		pr.leftMargin = ViewUtils.percentWidthToPixels(settings.getPosX())-(mWidth/2);
		pr.topMargin = ViewUtils.percentHeightToPixels(settings.getPosY())-(mHeight/2);
		Log.e("asd","Margins" +pr.leftMargin+", "+pr.topMargin+"measuredwidth "+ll.getMeasuredWidth());
		
		menuLayout.addView(ll, pr);
		menuLayout.invalidate();
		return menuLayout;
		
	}
	
	private void addSwitchView(MenuItem item, Menu menu,ViewGroup layout) {

		LinearLayout ll = new LinearLayout(this.cont);
		ll.setOrientation(item.getOrientation());
		ll.setGravity(item.getGravity());
		int width = LayoutParams.FILL_PARENT;
		int height = LayoutParams.WRAP_CONTENT;
		if((menu.getMenuSettings().getWidth() > 0))
			width = ViewUtils.percentWidthToPixels(menu.getMenuSettings().getWidth());
		
		if((menu.getMenuSettings().getHeight() > 0))
			height = ViewUtils.percentWidthToPixels(menu.getMenuSettings().getHeight());
		
		if(item.getHeight()>0){
			height = ViewUtils.percentWidthToPixels(item.getHeight());
		}
		if(item.getWidth()>0)
			width = ViewUtils.percentWidthToPixels(item.getWidth());

		TextView view = new TextView(cont);
		view.setText(item.getText());
		view.setTextColor(item.getTextColor());
		view.setTypeface(menu.getTypeFace());
		view.setId(item.getId());
		view.setTag(new Integer(item.getIdx()));
		view.setTextSize(item.getTextSize());
		StoryButton btn = new StoryButton(cont,item.getTag());
		//btn.setBackgroundColor(menu.getColor());
		btn.setChecked(true);
		//btn.setTextOn("On");
		//btn.setTextOff("Off");
		ll.setGravity(item.getGravity());
		btn.setTextColor(item.getTextColor());
		btn.setTextSize(item.getTextSize());
		btn.setTypeface(menu.getTypeFace());
		if(item.getOrientation() == LinearLayout.HORIZONTAL){
			ll.addView(view,new LinearLayout.LayoutParams(width/2,LayoutParams.FILL_PARENT));
			ll.addView(btn, new LinearLayout.LayoutParams(width/2,LayoutParams.FILL_PARENT));
		}
		if(item.getOrientation() == LinearLayout.VERTICAL){
			ll.addView(view,new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,height/2));
			ll.addView(btn, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,height/2));
		}
		btn.setOnCheckedChangeListener(listener);
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
		int margin = ViewUtils.percentWidthToPixels(2);
		params.setMargins(margin,margin,margin,margin);
		layout.addView(ll, params);
		
	}

	private void addAdjustmentView(MenuItem item,Menu menu, ViewGroup layout) {

		LinearLayout ll = new LinearLayout(this.cont);
		ll.setOrientation(item.getOrientation());
		ll.setGravity(item.getGravity());
		int width = LayoutParams.FILL_PARENT;
		int height = LayoutParams.WRAP_CONTENT;
		if((menu.getMenuSettings().getWidth() > 0))
			width = ViewUtils.percentWidthToPixels(menu.getMenuSettings().getWidth());
		
		if((menu.getMenuSettings().getHeight() > 0))
			height = ViewUtils.percentWidthToPixels(menu.getMenuSettings().getHeight());
		
		if(item.getHeight()>0){
			height = ViewUtils.percentWidthToPixels(item.getHeight());
		}
		if(item.getWidth()>0)
			width = ViewUtils.percentWidthToPixels(item.getWidth());

		TextView view = new TextView(cont);
		view.setText(item.getText());
		view.setTextColor(item.getTextColor());
		view.setTypeface(menu.getTypeFace());
		view.setId(item.getId());
		view.setTag(new Integer(item.getIdx()));
		view.setTextSize(item.getTextSize());
		StoryBar bar = new StoryBar(this.cont,item.getTag());
		//bar.setBackgroundColor(menu.getColor());
		bar.setMax(100);
		bar.setProgress(50);
		if(item.getOrientation() == LinearLayout.HORIZONTAL){
			ll.addView(view,new LinearLayout.LayoutParams(width/2,LayoutParams.FILL_PARENT));
			ll.addView(bar, new LinearLayout.LayoutParams(width/2,LayoutParams.FILL_PARENT));
		}
		if(item.getOrientation() == LinearLayout.VERTICAL){
			ll.addView(view,new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,height/2));
			ll.addView(bar, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,height/2));
		}
		bar.setOnSeekBarChangeListener(listener);
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
		int margin = ViewUtils.percentWidthToPixels(2);
		params.setMargins(margin,margin,margin,margin);
		layout.addView(ll, params);
		
	}

	private void addTextView(Menu menu, ViewGroup layout, MenuItem item, boolean addListener,boolean relative){
		TextView view = new TextView(cont);
		view.setText(item.getText());
		view.setTextColor(item.getTextColor());
		view.setTypeface(menu.getTypeFace());
		view.setId(item.getId());
		view.setTag(new Integer(item.getIdx()));
		view.setTextSize(item.getTextSize());
		view.setGravity(item.getGravity());
		view.setTag(item.getIdx());
		Log.e("TAG","Adding view with text "+item.getText()+" color "+item.getTextColor()+" textsize "+item.getTextSize()+" gravity "+item.getGravity());
		
		int pHeight = LayoutParams.WRAP_CONTENT;
		int pWidth = LayoutParams.WRAP_CONTENT;
		int mHeight = 0;
		int mWidth = 0;
		
		if((!relative) && (menu.getMenuSettings().getWidth() > 0))
			pWidth = mWidth = ViewUtils.percentWidthToPixels(menu.getMenuSettings().getWidth());
		
		if((!relative) && (menu.getMenuSettings().getHeight() > 0))
			pHeight = mHeight = ViewUtils.percentWidthToPixels(menu.getMenuSettings().getHeight());
		
		if(item.getHeight()>0){
			pHeight = mHeight = ViewUtils.percentWidthToPixels(item.getHeight());
		}
		if(item.getWidth()>0)
			pWidth = mWidth = ViewUtils.percentWidthToPixels(item.getWidth());

		if(relative){
			RelativeLayout.LayoutParams parameters = new RelativeLayout.LayoutParams(pWidth, pHeight);
			parameters.leftMargin = ViewUtils.percentWidthToPixels(item.getPosX())-(mWidth/2);
			parameters.topMargin = ViewUtils.percentHeightToPixels(item.getPosY())-(mHeight/2);
			layout.addView(view, parameters);
		}
		else{
			LinearLayout.LayoutParams parameters = new LinearLayout.LayoutParams(pWidth, pHeight);
			Log.e("tst","H:"+pHeight+"W:"+pWidth);
			view.setGravity(menu.getMenuSettings().getGravity());
			layout.addView(view,parameters);
				
		}
		if(addListener){
			
			view.setOnTouchListener(listener);
		}
	}

	
	

}
